package com.ruoyi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.SysPythonUrlEntity;
import com.ruoyi.system.domain.vo.PageVo;
import com.ruoyi.system.service.IMpSysPythonUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-01-19
 */
@RestController
@RequestMapping("/system/urlentity")
public class SysPythonUrlController extends BaseController {
    @Autowired
    IMpSysPythonUrlService iMpSysPythonUrlService;

    @Anonymous
    @PostMapping("/save")
    public AjaxResult save(@RequestBody SysPythonUrlEntity sysPythonUrlEntity){
        if (sysPythonUrlEntity.getId() != null) {
            sysPythonUrlEntity.setUpdateBy(getUsername());
            sysPythonUrlEntity.setUpdateTime(LocalDateTime.now());
        }else {
            sysPythonUrlEntity.setCreatTime(LocalDateTime.now());
            sysPythonUrlEntity.setCreatBy(getUsername());
        }
        return success(iMpSysPythonUrlService.saveAndUpdateUrlService(sysPythonUrlEntity));
    }
    @Anonymous
    @GetMapping("/url")
    public TableDataInfo searchSysUrl(SysPythonUrlEntity sysPythonUrlEntity, PageVo pageVo){
        PageInfo<SysPythonUrlEntity> sysPythonUrlEntityPageInfo = iMpSysPythonUrlService.searchSysPythonUrl(sysPythonUrlEntity,pageVo);
        TableDataInfo tableDataInfo = getDataTable(sysPythonUrlEntityPageInfo.getList());

        tableDataInfo.setTotal(sysPythonUrlEntityPageInfo.getTotal());

        return tableDataInfo;
    }

    /**
     * 开启或关闭爬虫
     * @param id
     * @param status
     * @return
     */
    @Anonymous
    @PutMapping("/onUrl")
    public AjaxResult searchOnUrl(@RequestBody SysPythonUrlEntity sysPythonUrlEntity){
        return  success(iMpSysPythonUrlService.onUrl(sysPythonUrlEntity.getId(),sysPythonUrlEntity.getStatus()));
    };

    /**
     * 删除爬虫
     * @param id
     * @return
     */
    @Anonymous
    @GetMapping("/delUrl")
    public AjaxResult delUrl(Integer id){
        return  success(iMpSysPythonUrlService.delUrl(id));
    }
}
